package cn.edu.nju.ws.sview.wyge.relations.adataset.datasetDocs;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

/**
 * input hints fetch the popular entities my entities
 * 
 * @author ssgong
 * 
 */
public class DatasetDocs {
	/**
	 * @param start
	 *            起 始位置
	 * @param length
	 *            长度
	 * @return 排序列表（包括bookmark的entity，原始的entity和集合）中从start开始的总共length个元素
	 */
	public static ArrayList<DatasetItem> fetchAll(int start, int length,
			String lang, int ds_id) {
		ArrayList<DatasetItem> items = new ArrayList<DatasetItem>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			String str = String
					.format("SELECT doc_id FROM custom_dataset_doc where dataset_id = %d order by doc_id limit %d, %d",
							ds_id, start, length);
			ResultSet rs = stmt.executeQuery(str);
			while (rs.next()) {
				String dsType = "uri";
				int dsId = rs.getInt(1);
				String uri = null;
				try {
					uri = URIIDCache.getInstance().getURI(dsId);
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				items.add(new DatasetItem(dsType, dsId, uri));
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return items;
	}
}
